package com.example.ws.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.stereotype.Component;

import java.util.Set;

@Component
@Slf4j
public class RedisMessageSubscriber implements MessageListener {
    @Autowired
    private IOnlineUserService onlineUserService;
    @Override
    public void onMessage(Message message, byte[] bytes) {
       String channel = new String(message.getChannel());
       String body = new String(message.getBody());
        log.info("收到订阅消息-----频道：{}，内容：{}",channel,body);

        if ("在线用户列表".equals(body)){
            Set<String> users = onlineUserService.getOnlineUsers();
            String response = "在线用户列表：" + String.join(",",users);
        }
    }
}
